init(50,"mylegend",720,1136,main);
var appId = '', openId = '', tel = '';
if(document.URL.lastIndexOf("?") > 0) {
    var arr = document.URL.substring(document.URL.lastIndexOf("?")+1, document.URL.length).split("&");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].split("=")[0] == 'appId') {
            appId = arr[i].split("=")[1];
        } else if (arr[i].split("=")[0] == 'openId') {
            openId = arr[i].split("=")[1];
        } else if (arr[i].split("=")[0] == 'tel') {
            tel = arr[i].split("=")[1].split("#")[0];
        }
    }
}
var maskLayer=new LSprite(), popupLayer, popupMsgLeft = 57, popupMsgTop = 305, popupMsgLineHeight = 45, popupBtnTop = 455;


var loadingLayer, backLayer, stopLayer, startLayer;

var imglist = {};

var imgData = new Array();


var combination = new Array(
    [1,1,2], [1,1,3], [1,1,4], [1,1,5], [1,1,6], [1,1,7],
    [1,2,1], [1,3,1], [1,4,1], [1,5,1], [1,6,1], [1,7,1],
    [2,1,1], [3,1,1], [4,1,1], [5,1,1], [6,1,1], [7,1,1],

    [2,2,1], [2,2,3], [2,2,4], [2,2,5], [2,2,6], [2,2,7],
    [2,1,2], [2,3,2], [2,4,2], [2,5,2], [2,6,2], [2,7,2],
    [1,2,2], [3,2,2], [4,2,2], [5,2,2], [6,2,2], [7,2,2],

    [3,3,1], [3,3,2], [3,3,4], [3,3,5], [3,3,6], [3,3,7],
    [3,1,3], [3,2,3], [3,4,3], [3,5,3], [3,6,3], [3,7,3],
    [1,3,3], [2,3,3], [4,3,3], [5,3,3], [6,3,3], [7,3,3],

    [4,4,1], [4,4,2], [4,4,3], [4,4,5], [4,4,6], [4,4,7],
    [4,1,4], [4,2,4], [4,3,4], [4,5,4], [4,6,4], [4,7,4],
    [1,4,4], [2,4,4], [3,4,4], [5,4,4], [6,4,4], [7,4,4],

    [5,5,1], [5,5,2], [5,5,3], [5,5,4], [5,5,6], [5,5,7],
    [5,1,5], [5,2,5], [5,3,5], [5,4,5], [5,6,5], [5,7,5],
    [1,5,5], [2,5,5], [3,5,5], [4,5,5], [6,5,5], [7,5,5],

    [6,6,1], [6,6,2], [6,6,3], [6,6,4], [6,6,5], [6,6,7],
    [6,1,6], [6,2,6], [6,3,6], [6,4,6], [6,5,6],
    [1,6,6], [2,6,6], [3,6,6], [4,6,6], [5,6,6], [7,6,6],

    [7,7,1], [7,7,2], [7,7,3], [7,7,4], [7,7,5], [7,7,6],
    [7,1,7], [7,2,7], [7,3,7], [7,4,7], [7,5,7], [7,6,7],
    [1,7,7], [2,7,7], [3,7,7], [4,7,7], [5,7,7]
);

var reels = new Array();
var kakes = new Array();
var stopBtn = new Array();
var start, score, rule;

var ruleUrl = "http://www.bing.com";

function main(){

	imgData.push({name:"stop_up",path:"./images/slot_stop_up.png"});

	imgData.push({name:"stop_over",path:"./images/slot_stop_over.png"});

	imgData.push({name:"start",path:"./images/slot_start.png"});

	imgData.push({name:"kake",path:"./images/slot_kake.png"});

	imgData.push({name:"slot_back",path:"./images/slot_back.png"});

	imgData.push({name:"score",path:"./images/bt_cj.png"});
	imgData.push({name:"rule",path:"./images/bt_rule.png"});

	imgData.push({name:"cancel",path:"./images/bt_box_cancel.png"});
	imgData.push({name:"done",path:"./images/bt_box_done.png"});

	imgData.push({name:"item01",path:"./images/01.png"});
	imgData.push({name:"item02",path:"./images/02.png"});
	imgData.push({name:"item03",path:"./images/03.png"});
	imgData.push({name:"item04",path:"./images/04.png"});
	imgData.push({name:"item05",path:"./images/05.png"});
	imgData.push({name:"item06",path:"./images/06.png"});
	imgData.push({name:"item07",path:"./images/07.png"});

	imgData.push({name:"item11",path:"./images/11.png"});
	imgData.push({name:"item12",path:"./images/12.png"});
	imgData.push({name:"item13",path:"./images/13.png"});
	imgData.push({name:"item14",path:"./images/14.png"});
	imgData.push({name:"item15",path:"./images/15.png"});
	imgData.push({name:"item16",path:"./images/16.png"});
	imgData.push({name:"item17",path:"./images/17.png"});

	imgData.push({name:"item21",path:"./images/21.png"});
	imgData.push({name:"item22",path:"./images/22.png"});
	imgData.push({name:"item23",path:"./images/23.png"});
	imgData.push({name:"item24",path:"./images/24.png"});
	imgData.push({name:"item25",path:"./images/25.png"});
	imgData.push({name:"item26",path:"./images/26.png"});
	imgData.push({name:"item27",path:"./images/27.png"});

	loadingLayer = new LoadingSample1();
	addChild(loadingLayer);	
	LLoadManage.load(
		imgData,
		function(progress){
			loadingLayer.setProgress(progress);
		},
		function(result){
			imglist = result;
			removeChild(loadingLayer);
			loadingLayer = null;
			gameInit();
		}
	);
}

function gameInit(event){

	var i,bitmap,bitmapdata;

	backLayer = new LSprite();
	addChild(backLayer);

	bitmapdata = new LBitmapData(imglist["slot_back"]);
	bitmap = new LBitmap(bitmapdata);
	backLayer.addChild(bitmap);

	stopLayer = new LSprite();
	addChild(stopLayer);

	for(i=0; i<3; i++) {
		var reel = new Reel(combination, i);
		reel.x = 178 * i + 120;
		reel.y = 486;
		reels.push(reel);
		addChild(reel);

		var kake = new LBitmap(new LBitmapData(imglist["kake"]));
		kake.x = 178 * i + 110;
		kake.y = 486;
		kakes.push(kake);
		addChild(kake);

        var stop = new LButton(new LBitmap(new LBitmapData(imglist["stop_over"])),new LBitmap(new LBitmapData(imglist["stop_up"])));
        stop.x = 205 * i + 63;
        stop.y = 842;
        stop.index = i;
        stopBtn.push(stop);
        stop.visible = false;
        stop.addEventListener(LMouseEvent.MOUSE_UP, stopEvent);
        addChild(stop);
	}

	startLayer = new LSprite();
	addChild(startLayer);
	start = new LButton(new LBitmap(new LBitmapData(imglist["start"])),new LBitmap(new LBitmapData(imglist["start"])));
	start.x = 63;
	start.y = 842;
	startLayer.addChild(start);
	start.addEventListener(LMouseEvent.MOUSE_UP, startEvent);

    score = new LButton(new LBitmap(new LBitmapData(imglist["score"])));
    score.x = 63;
    score.y = 1017;
    addChild(score);
    score.addEventListener(LMouseEvent.MOUSE_UP, function(){
        window.location.href = "takePrize.html?appId=" + appId + "&openId=" + openId;
    });

    rule = new LButton(new LBitmap(new LBitmapData(imglist["rule"])));
    rule.x = 378;
    rule.y = 1017;
    addChild(rule);
    rule.addEventListener(LMouseEvent.MOUSE_UP, function() {
        window.location = ruleUrl;
    });

	backLayer.addEventListener(LEvent.ENTER_FRAME, onframe);
}

var prizeResult = "";
function startEvent(event){
    start.removeEventListener(LMouseEvent.MOUSE_UP, startEvent);
    $.ajax({
        type: 'POST',
        url: '/rest/slot/end',
        data: {"appId": appId, "openId": openId, "tel": tel},
        success: function(data){
            data = eval('(' + data + ')');
            prizeResult = data;
            if(data.id == "") {
                showPopup(function (layer) {
                    layer.addChild(buildTextField(data.title, popupMsgLeft, popupMsgTop));
                    var details = prizeResult.details.split("，");
                    for(i=0; i<details.length; i++) {
                        layer.addChild(buildTextField(details[i], popupMsgLeft, popupMsgTop + (i+1) * popupMsgLineHeight));
                    }
                    layer.addChild(buildButton(buildBitmap("done"), popupMsgLeft + 158, popupBtnTop, function () {
                        closePopup();
                    }));
                });
            } else {
                var newCombination;
                if(data.id == "0") {
                    newCombination = combination[Math.floor(Math.random() * (combination.length / 3))];
                } else {
                    newCombination = data.prize.split(",");
                }
                start.visible = false;
                var i;
                for (i = 0; i < 3; i++) {
                    stopBtn[i].visible = true;
                    reels[i].startReel = true;
                    reels[i].stopFlag = false;
                    reels[i].stopNum = 0;
                    reels[i].combination = [newCombination];
                }
            }
        },
        complete: function(XMLHttpRequest, textStatus) {
            start.addEventListener(LMouseEvent.MOUSE_UP, startEvent);
        }
    });
}

function stopEvent(event,currentTarget){
    reels[currentTarget.index].stopFlag = true;
}

function onframe(){
	var i;
	for(i=0; i<3; i++){
		reels[i].onframe();
	}
}

function checkWin(){
	var i;
	var allstop = 0;
	for(i=0; i<3; i++) {
		if(!reels[i].startReel) {
			allstop++;
		}
	}

	if(allstop >= 3){
        for(i=0;i<3;i++){
            stopBtn[i].visible = false;
        }

		if(reels[0].stopNum == 0){
			//win.visible = true;
            showPopup(function (layer) {
                layer.addChild(buildTextField(prizeResult.title, popupMsgLeft, popupMsgTop));
                var details = prizeResult.details.split("，");
                for(i=0; i<details.length; i++) {
                    layer.addChild(buildTextField(details[i], popupMsgLeft, popupMsgTop + (i+1) * popupMsgLineHeight));
                }
                layer.addChild(buildButton(buildBitmap("done"), popupMsgLeft + 158, popupBtnTop, function () {
                    closePopup();
                    start.visible = true;
                }));
            });
		}else{
			start.visible = true;
		}
	}
}

function winClick(){
	win.visible = false;
	start.visible = true;
}

var canvasWidth = 720, canvasHeight = 1136;
function showPopup(fnContent, isChangeContent) {
    if(!isChangeContent) {
        maskLayer.graphics.drawRect(0, "white", [0, 0, canvasWidth, canvasHeight], true, "#fcad26");
        maskLayer.alpha = 0;
        startLayer.addChild(maskLayer);

        popupLayer = new LSprite();
        popupLayer.graphics.drawRoundRect(5, "white", [18, 288, 682, 270, 15], true, "#F10142");
        startLayer.addChild(popupLayer);
    }

    fnContent(popupLayer);
}

function closePopup(keepMask) {
    if(popupLayer) {
        popupLayer.removeAllChild();
        popupLayer.die();
    }
    if(!keepMask) {
        maskLayer.die();
    }
}

function buildTextField(text, x, y, size, color) {
    var textField = new LTextField();
    textField.weight = "bold";
    textField.text = text;
    textField.x = x ? x : 0;
    textField.y = y ? y : 0;
    textField.size = size ? size : 30;
    textField.color = color ? color : "white";
    return textField;
}

function buildButton(bitmap, x, y, fnEvent, disableBitmap) {
    var button = new LButton(bitmap, bitmap, bitmap, disableBitmap);
    button.x = x ? x : 0;
    button.y = y ? y : 0;
    button.addEventListener(LMouseEvent.MOUSE_UP, fnEvent);
    return button;
}

function buildBitmap(imgName, x, y, scale) {
    var bitmap = new LBitmap(new LBitmapData(imglist[imgName]));
    bitmap.x = x ? x : 0;
    bitmap.y = y ? y : 0;
    scale = scale ? scale : 1;
    bitmap.scaleX = scale;
    bitmap.scaleY = scale;
    return bitmap;
}
